<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../common/EasyMysql.php");
require_once(dirname(__FILE__) . "/../config/ConfigDB.php");

class ServiceinfoCom{
    function __construct() {
        $dbconfig = $GLOBALS["config"] ['database'];
        $mysql = new EasyMysql($dbconfig['hostname'], $dbconfig ['port'], $dbconfig ['username'], $dbconfig ['password'], $dbconfig ['db'], $dbconfig ['charset']);
        $this->mysql = $mysql;
    }
    function addServiceinfo($orderid,$serviceid,$total,$isncard){
        try {
            $create_time = time();
            $sql = "insert into serviceinfo(order_id,service_id,total,isncard) values({$orderid},{$serviceid},{$total},{$isncard});SELECT @@Identity";
            $ret = $this->mysql->query($sql);
            return $ret[0][0]['@@Identity'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    
    function getServiceinfoByOrderid($orderid){
        try {
            $sql = "select * from serviceinfo where order_id={$orderid}";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getServiceinfoByOids($orderids){
        try {
            $sql = "select * from serviceinfo where order_id in ({$orderids}) and isncard=1";
            $ret = $this->mysql->query($sql);
            if (!empty($ret) && !empty($ret [0]) && !empty($ret [0][0])) {
                return $ret[0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return null;
    }
    
    function getNumTem(){
        try{
            $sql = "select count(1) from serviceinfo where used_num<2 and isncard=0";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumDou(){
        try{
            $sql = "select count(1) from serviceinfo where used_num>1 and isncard=0";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumNcard(){
        try{
            $sql = "select count(1) from serviceinfo where isncard=1";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getNumService($service){
        try{
            $sql = "select count(1) from serviceinfo where service_id in ({$service})";
            $ret = $this->mysql->query($sql);
            return $ret [0][0]['count(1)'];
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function getAllNum(){
        try{
            $sql = "select count(1) from serviceinfo";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0]['count(1)'];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
    function changeUsedByOrderid($orderid,$usednum){
        try {
            $create_time = time();
            $sql = "update serviceinfo set used_num={$usednum} where order_id={$orderid}";
            $ret = $this->mysql->query($sql);
            return true;
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return false;
    }
    function getOldUsednum($orderid){
        try{
            $sql = "select * from serviceinfo where order_id={$orderid}";
            $ret = $this->mysql->query($sql);
            if (!empty ($ret) && !empty ($ret [0]) && !empty ($ret [0][0])) {
                return $ret [0][0];
            }
        } catch (Exception $ex) {
            //throw new Exception ("AppDao.fetchList: " . $ex->getMessage());
            return false;
        }
        return 0;
    }
}
